package com.nutricia.biz.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.nutricia.common.api.request.AddExternalPeopleReq;
import com.nutricia.common.api.request.AddInsidePeopleReq;
import com.nutricia.common.api.request.QueryMeetingReq;
import com.nutricia.common.api.request.QueryOptionExternalByMidReq;
import com.nutricia.common.api.request.QueryOptionInsideByMidReq;
import com.nutricia.common.api.result.QueryMeeting;
import com.nutricia.common.api.result.QueryMeetingRes;
import com.nutricia.common.api.result.QueryOptionExternalByMidRes;
import com.nutricia.common.api.result.QueryOptionInsideByMid;
import com.nutricia.common.api.result.ReturnDataRes;
import com.nutricia.common.lang.ServiceCallBack;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.model.domain.MeetingDO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value="/meetingSignUp", description="会议报名相关接口")
@RestController
@RequestMapping("/meetingSignUp")
public class MeetingSignUpController extends AbstractFacadeImpl{
	
	@ApiOperation(value="queryMeeting", notes="查询会议信息")
	@RequestMapping(value="queryMeeting", method=RequestMethod.POST)
	public ReturnDataRes queryMeeting(@RequestBody QueryMeetingReq queryMeetingReq) {
		 final ReturnDataRes result = new ReturnDataRes();
		 return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
	            @Override
	            public void check() throws Exception {
	            	CheckUtil.isNotBlank(queryMeetingReq.getmId(),"会议ID为空！");
	            }

	            @Override
	            public void doService() throws Exception {
	            	//查询会议信息详情
	            	MeetingDO meetingDO = meetingSignUpService.queryMeeting(queryMeetingReq.getmId());
	            	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	            	QueryMeeting queryMeeting = new QueryMeeting();
	            	queryMeeting.setmId(meetingDO.getmId());
	            	queryMeeting.setmName(meetingDO.getmName());
	            	queryMeeting.setmStartDate(sdf.format(meetingDO.getmStartDate()).toString());
	            	queryMeeting.setmEndDate(sdf.format(meetingDO.getmEndDate()).toString());
	            	queryMeeting.setmHostCity(meetingDO.getmHostCity());
	            	queryMeeting.setHotel(meetingDO.getHotel());
	            	queryMeeting.setAddress(meetingDO.getAddress());
	            	queryMeeting.setContacts(meetingDO.getContacts());
	            	queryMeeting.setContactNumber(meetingDO.getContactNumber());
	            	queryMeeting.setContactEmail(meetingDO.getContactEmail());
	            	queryMeeting.setmNatures(meetingDO.getmNatures());
	            	queryMeeting.setmBriefIntr(meetingDO.getmBriefIntr());
	            	
	            	QueryMeetingRes queryMeetingRes = new QueryMeetingRes();
	            	queryMeetingRes.setQueryMeeting(queryMeeting);
	            	
	            	//获取参会内部人员信息
	            	queryMeetingRes.setQueryMeetingIpeopleList(meetingSignUpService.queryMeetingIpeople(
	            			queryMeetingReq.getmId()));
	            	
	            	//获取外部人员参会信息
	            	queryMeetingRes.setQueryMeetingEpeopleList(meetingSignUpService.queryMeetingEpeople(
	            			queryMeetingReq.getmId()));
	            	
	            	result.setData(queryMeetingRes);
	            	
	            	result.setCode("0000");
	            	result.setSuccess(true);
	            	result.setMessage("查询会议信息成功！");
	            }
	        });
	}

	@ApiOperation(value="addInsidePeople", notes="新增会议内部参会人员")
	@RequestMapping(value="addInsidePeople", method=RequestMethod.POST)
	public ReturnDataRes addInsidePeople(@RequestBody AddInsidePeopleReq addInsidePeopleReq) {
		 final ReturnDataRes result = new ReturnDataRes();
		 return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
	            @Override
	            public void check() throws Exception {
	            	 CheckUtil.isNotBlank(addInsidePeopleReq.getMeetingId(),"会议ID为空！");
	            	 CheckUtil.isNotBlank(addInsidePeopleReq.getCaacCertificateCode(),"证件号为空！");
	            }

	            @Override
	            public void doService() throws Exception {
	            	String iPeopleId = meetingSignUpService.addInsidePeople(addInsidePeopleReq);
	            	 result.setData(iPeopleId);
		                if(iPeopleId != null) {
		                	result.setMessage("会议内部参会人员新增成功！");
		 	                result.setCode("0000");
		                }else{
		                	result.setMessage("会议内部参会人员新增失败！");
		 	                result.setCode("1111");
		                }
		                result.setSuccess(true);
	            }
	        });
	}

	@ApiOperation(value="queryOptionInsideByMid", notes="新增会议内部参会人员时查询会议所有选项")
	@RequestMapping(value="queryOptionInsideByMid", method=RequestMethod.POST)
	public ReturnDataRes queryOptionInsideByMid(
			@RequestBody QueryOptionInsideByMidReq queryOptionInsideByMidReq) {
		 final ReturnDataRes result = new ReturnDataRes();
		 return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
	            @Override
	            public void check() throws Exception {
	            	CheckUtil.isNotBlank(queryOptionInsideByMidReq.getMeetingId(),"会议ID为空！");
	            }

	            @Override
	            public void doService() throws Exception {
	            	List<QueryOptionInsideByMid> queryOptionInsideByMidList = 
	            			meetingSignUpService.queryOptionInsideByMid(queryOptionInsideByMidReq.
	            					getMeetingId());
	            	result.setData(queryOptionInsideByMidList);
	            	result.setMessage("新增会议内部参会人员 获取会议所有选项成功！");
 	                result.setCode("0000");
 	                result.setSuccess(true);
	            }
	        });
	}

	@ApiOperation(value="queryOptionExternalByMid", notes="新增会议外部参会人员时查询会议所有选项和联系人信息")
	@RequestMapping(value="queryOptionExternalByMid", method=RequestMethod.POST)
	public ReturnDataRes queryOptionExternalByMid(
			@RequestBody QueryOptionExternalByMidReq queryOptionExternalByMidReq) {
		final ReturnDataRes result = new ReturnDataRes();
		return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            	CheckUtil.isNotBlank(queryOptionExternalByMidReq.getMeetingId(),"会议ID为空！");
            }

            @Override
            public void doService() throws Exception {
            	QueryOptionExternalByMidRes queryOptionExternalByMidRes = 
            			meetingSignUpService.queryOptionContactsByMid(queryOptionExternalByMidReq.
            					getMeetingId());
            	
            	result.setData(queryOptionExternalByMidRes);
            	result.setMessage("新增会议外部参会人员 获取会议所有选项和联系人信息成功！");
	            result.setCode("0000");
	            result.setSuccess(true);
            }
        });
	}

	@ApiOperation(value="addExternalPeople", notes="新增会议外部参会人员")
	@RequestMapping(value="addExternalPeople", method=RequestMethod.POST)
	public ReturnDataRes addExternalPeople(@RequestBody AddExternalPeopleReq addExternalPeopleReq) {
		final ReturnDataRes result = new ReturnDataRes();
		return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            	CheckUtil.isNotBlank(addExternalPeopleReq.getMeetingId(),"会议ID为空！");
           	    CheckUtil.isNotBlank(addExternalPeopleReq.getCaacCertificateCode(),"证件号为空！");
            }

            @Override
            public void doService() throws Exception {
            	String ePeopleId = meetingSignUpService.addExternalPeople(addExternalPeopleReq);
            	result.setData(ePeopleId);
                if(ePeopleId != null) {
                	result.setMessage("会议外部参会人员新增成功！");
 	                result.setCode("0000");
                }else{
                	result.setMessage("会议外部参会人员新增失败！");
 	                result.setCode("1111");
                }
                result.setSuccess(true);
            }
        });
	}
}
